require "singleton"

class SimpleLogger
  include Singleton
  
  attr_accessor :level
  
  ERROR, WARING, INFO = 1, 2, 3
  
  @@instance = SimpleLogger.new
  
  def initialize
    @log = File.open(File.expand_path("../log.txt", __FILE__), "w")
    @level = WARNING
  end
  
  def error(msg)
    @log.puts msg
    @log.flush
  end
  
  def warning(msg)
    @log.puts(msg) if @level >= WARNING
    @log.flush
  end
  
  def info(msg)
    @log.puts(msg) if @level >= INFO
    @log.flush
  end
  
  class << self
    def instance
      @@instance
    end
  end
  
  private_class_method :new
end

logger = SimpleLogger.instance
logger.level = SimpleLogger::INFO

logger.info "Doing the first thing"
logger.info "Now doing the second thing"

class ClassBasedLogger
  ERROR, WARNING, INFO = 1, 2, 3
  
  @@log = File.open(File.expand_path('../new_log.txt', __FILE__), 'w')
  @@level = WARNING
  
  class << self
    def error(msg)
      @@log.puts msg
      @@log.flush
    end
    
    def warning(msg)
      @@log.puts(msg) if @@level >= WARNING
      @@log.flush
    end
    
    def info(msg)
      @@log.puts(msg) if @@level >= INFO
      @@log.flush
    end
    
    def level=(new_level)
      @@level = new_level
    end
    
    def level
      @@level
    end
  end
end